package com.auleev.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexFoo {

  public static void main(String[] args) {
      String regex = "/fr/croisieres/[\\w-]*/destination,([0-9]*),([0-9]*)/?(.*)$";
     
      String compagnieRegex = "/fr/croisieres/[\\w-]*/compagnie,([0-9]*)/$";
      String text = "/fr/croisieres/croisiere/destination,0,0";
      System.out.println(IsMatch(text,regex)); // true
      System.out.println(IsMatch(text,compagnieRegex)); // false
      
      String SUBDOMAIN_PATTERN = "(?:(?!www).)*\\.(?:abcroisiere|crocierepromo|crucerospromo|promokreuzfahrten).*/([a-zA-Z_]{2,20})/.*$";
      String urlSubdomainMSC = "msc.abcroisiere.com";
      String urlABC="abcroisiere.com";
      System.out.println("subdomain");
      System.out.println(IsMatch(urlSubdomainMSC, SUBDOMAIN_PATTERN));
      
      String SubDomain = "(?i:[a-z0-9];[a-z0-9][-a-z0-9]+[a-z0-9])";
      String TopDomains = "(?x-i:com\\b \n" +
          " ;edu\\b  \n" +
          " ;biz\\b  \n" +
          " ;in(?:t;fo)\\b \n" +
          " ;mil\\b  \n" +
          " ;net\\b  \n" +
          " ;org\\b  \n" +
          " ;[a-z][a-z]\\b \n" + // country codes
          ")  \n";
      String Hostname = "(?:" + SubDomain + "\\.)+" + TopDomains;
      System.out.println(IsMatch(urlSubdomainMSC, Hostname));
}

  /**
   * @param s
   * @param pattern
   * @return
   */
  private static boolean IsMatch(String s, String pattern) {
      try {
          Pattern patt = Pattern.compile(pattern);
          Matcher matcher = patt.matcher(s);
          return matcher.matches();
      } catch (RuntimeException e) {
      return false;
  }       
}   
  
}